package net.valerijtimakov.homefinances.dao.storages.finances.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import net.valerijtimakov.homefinances.domain.common.DataItem;
import net.valerijtimakov.homefinances.domain.finances.IncomingItem;
import net.valerijtimakov.homefinances.exceptions.AppExceptionWrongState;
import net.valerijtimakov.homefinances.exceptions.AppPersProviderException;
import net.valerijtimakov.homefinances.exceptions.AppPersistentException;
import net.valerijtimakov.homefinances.exceptions.wrongarg.AppExceptionNullArgument;
import net.valerijtimakov.homefinances.dao.storages.common.mysql.OperationItemsStorage;
import net.valerijtimakov.homefinances.dao.storages.common.mysql.query_builders.condition.WhereBuilder;
import net.valerijtimakov.homefinances.dao.storages.finances.IIncomingsStorage;

public class IncomingsStorage extends OperationItemsStorage implements IIncomingsStorage {
	
	protected String incomingsFieldName = "incomings";

	public IncomingsStorage(IMySqlProvider provider) throws AppExceptionWrongState {
		super(provider);
	}

	public Map<Long, IncomingItem> filterIncomings(long[] accountsIds, long[] cathegoriesIds, long[] currenciesIds, 
		Date addDateFrom, Date addDateTo, final float incomingFrom, final float incomingTo,	String namePattern) 
		throws AppPersProviderException, AppExceptionWrongState 
	{
		return super.filterCosts(accountsIds, cathegoriesIds, currenciesIds, addDateFrom, addDateTo, namePattern, 
			new FiltersAdditor() {
				public void addFilters(WhereBuilder whereBuilder) {
					whereBuilder.addIntervalCondBuilder(incomingsFieldName, incomingFrom, incomingTo);
				}
			});
	}

	public Map<Long, IncomingItem> getAccountsIncomings(long... accountId) throws AppPersProviderException, AppExceptionWrongState {
		return getAccountOperations(accountId);
	}

	public Map<Long, IncomingItem> getIncomings(long... id) throws AppPersProviderException, AppExceptionWrongState {
		return getItemsByIds(id);
	}

	public void addIncoming(IncomingItem item) throws AppExceptionNullArgument, AppExceptionWrongState, AppPersistentException {
		addItem(item, "Не задан экземпляр дохода!", "Ошибка добавления дохода!");
	}

	public void removeIncoming(long id) throws AppPersProviderException, AppExceptionWrongState {
		delete(id);
	}

	public void updateIncoming(IncomingItem item) throws AppPersProviderException, AppExceptionWrongState {
		update(item);
	}

	@Override
	protected String getTableName() {
		return "incomings";
	}

	@Override
	protected DataItem createTempItem() {
		return new IncomingItem();
	}

	@Override
	protected <T extends DataItem> void parse(ResultSet rs, T item) throws SQLException {
		super.parse(rs, item);
		IncomingItem cItem = (IncomingItem)item;
		cItem.setIncom(rs.getFloat(incomingsFieldName));
	}
	
	@Override
	protected void initSelect() {
		super.initSelect();
		selectQuery.addField(incomingsFieldName);
	}

	@Override
	protected <T extends DataItem> void addData(T item) {
		super.addData(item);
		IncomingItem cItem = (IncomingItem)item;
		insertQuery.setFieldValue(incomingsFieldName, cItem.getIncom());
	}

	@Override
	protected <T extends DataItem> void setUpdatedFields(T item) {
		super.setUpdatedFields(item);
		IncomingItem cItem = (IncomingItem)item;
		updateQuery.setFieldValue(incomingsFieldName, cItem.getIncom());
	}

}
